package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.RoundAngleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class TuiJianAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;

	public TuiJianAdapter(Activity activity, List<JSONObject> itemList) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public RoundAngleImageView image;
		public TextView date,title,info,detail,nomore;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int main_tandian_item = R.layout.tuijian_item;
			convertView = LayoutInflater.from(appContext).inflate(main_tandian_item, null);
			holder.image = (RoundAngleImageView) convertView.findViewById(R.id.tuijian_item_image);
			int height = (UIUtil.winWidth-UIUtil.dip2px(appContext, 20))*300/600;
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height);
			holder.image.setLayoutParams(lp);
			holder.date = (TextView) convertView.findViewById(R.id.tuijian_item_date);
			holder.title = (TextView) convertView.findViewById(R.id.tuijian_item_title);
			holder.info = (TextView) convertView.findViewById(R.id.tuijian_item_info);
			holder.detail = (TextView) convertView.findViewById(R.id.tuijian_item_detaile);
			holder.nomore = (TextView) convertView.findViewById(R.id.tuijian_item_nomore);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		final JSONObject json = itemList.get(position);
		try {
			holder.nomore.setVisibility(position==itemList.size()-1&&pageEnd?View.VISIBLE:View.GONE);
			ImageLoader.getInstance().displayImage(BaseTools.getFrom("image_url", json), holder.image, UIUtil.getImageLoaderDisplayOptions(R.drawable.icon_default_f));
			holder.title.setText(BaseTools.getFrom("title", json));
			holder.date.setText(BaseTools.getFrom("startenddate", json));
			holder.info.setText(BaseTools.getFrom("status", json));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return convertView;
	}
}
